// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED!
// YOU MAY USE THIS CODE: HOWEVER THIS GRANTS NO FUTURE RIGHTS.
// see http://telnetcsharp.codeplex.com/ for further details and license information
using System;
using System.Net;

namespace TelnetCSharp.Demo
{
    /// <summary>
    /// Demo for the telnet class, see http://telnetcsharp.codeplex.com/documentation for 
    /// further details.
    /// </summary>
    public class TerminalDemo
    {
        /// <summary>
        /// The main entry point for the application.
        /// Can be used to test the programm and run it from command line.
        /// </summary>
        // ReSharper disable UnusedMember.Local
        [STAThread]
        private static void Main(string[] args)
        {
             DemoMSTelnetServer(args);
            // DemoRH73TelnetServer(args);
           // DemoRt311Router(args);
        }
        // ReSharper restore UnusedMember.Local

        /// <summary>
        /// Demo for a MS Telnet server
        /// </summary>
        // ReSharper disable UnusedMember.Local
        // ReSharper disable UnusedParameter.Local
        private static void DemoMSTelnetServer(string[] args)
        {
            var tn = new Terminal("127.0.0.1", 23, 10, 80, 40); // hostname, port, timeout [s], width, height
            tn.Connect(); // physcial connection
            do
            {
                string f = tn.WaitForString("Login");
                if (f == null)
                    throw new TerminalException("No login possible");
                Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
                tn.SendResponse("telnet", true); // send username
                f = tn.WaitForString("Password");
                if (f == null)
                    throw new TerminalException("No password prompt found");
                Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
                tn.SendResponse("telnet", true); // send password 
                f = tn.WaitForString(">");
                if (f == null)
                    throw new TerminalException("No > prompt found");
                tn.SendResponse("dir", true); // send dir command
                if (tn.WaitForChangedScreen())
                    Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
                BatchProcessDemo b = new BatchProcessDemo(tn, "mybat.bat", new Uri("ftp://172.16.16.20/myb.bat"), "c:\\", (ICredentials)new NetworkCredential("SHASHANK-AC86C3\\Telnet", "shashank185")); ;
                b.ExecuteScript();
            }
            while (false);
            tn.Close(); // physically close on TcpClient
            Console.WriteLine("\n\nEnter to continue ...\n");
            Console.ReadLine();
        }
        // ReSharper restore UnusedParameter.Local
        // ReSharper restore UnusedMember.Local

        /// <summary>
        /// Demo for a Linux RedHat 7.3 telnet server
        /// </summary>
        // ReSharper disable UnusedMember.Local
        // ReSharper disable UnusedParameter.Local
        private static void DemoRh73TelnetServer(string[] args)
        {
            var tn = new Terminal("10.10.20.140", 23, 10, 80, 40); // hostname, port, timeout [s], width, height
            tn.Connect(); // physcial connection
            do
            {
                string f = tn.WaitForString("Login");
                if (f == null)
                    break; // this little clumsy line is better to watch in the debugger
                Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
                tn.SendResponse("kba", true); // send username
                f = tn.WaitForString("Password");
                if (f == null)
                    break;
                Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
                tn.SendResponse("vmware", true); // send password 
                f = tn.WaitForString("$"); // bash
                if (f == null)
                    break;
                tn.SendResponse("df", true); // send Shell command
                if (tn.WaitForChangedScreen())
                    Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
            }
            while (false);
            tn.Close(); // physically close on TcpClient
            Console.WriteLine("\n\nEnter to continue ...\n");
            Console.ReadLine();
        }
        // ReSharper restore UnusedParameter.Local
        // ReSharper restore UnusedMember.Local
 
        /// <summary>
        /// Demo for a RT311 Router
        /// </summary>
        // ReSharper disable UnusedParameter.Local
        private static void DemoRt311Router(string[] args)
        {
            var tn = new Terminal("router", 23, 10, 80, 40); // hostname, port, timeout [s], width, height
            tn.Connect(); // physcial connection
            do
            {
                string f = tn.WaitForString("Password");
                if (f == null)
                    throw new TerminalException("No password prompt found");
                Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
                tn.SendResponse("1234", true); // send password
                f = tn.WaitForString("Enter");
                if (f == null)
                    throw new TerminalException("No 1st menu screen found");
                Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
                tn.SendResponse("24", true); // send "24" to get to next screen 
                f = tn.WaitForString("Enter", false, 30); // String, case sensitive, timeout
                if (f == null)
                    throw new TerminalException("No 2nd menu screen found");
                Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
                tn.SendResponse("1", true); // send "1" to get to next screen
                Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
                f = Terminal.FindIpAddress(tn.WaitForRegEx(@"WAN.+?(\d?\d?\d\.\d?\d?\d\.\d?\d?\d\.\d?\d?\d)")); // search for 1st IP-like address next to "WAN"
                if (f == null)
                    throw new TerminalException("No IP address found");
                Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
                Console.WriteLine("\n\nEXTERNAL IP " + f);
            }
            while (false);
            tn.SendLogout(true);
            tn.Close(); // physically close on TcpClient
            Console.WriteLine("\n\nEnter to continue ...\n");
            Console.ReadLine();
        }
        // ReSharper restore UnusedParameter.Local
    } // class
}

// namespace